const WXAPI = require('apifm-wxapi')

Page({
  onLoad: function(options) {
    console.log("获取库存合同："+options.stockId)
    this.setData({
      stockId : options.stockId
    })
    
  },

  onShow: function(){
    // 进入界面刷新数据
    this.getContract();
  },

  // 获取合同
  async getContract(){
    var stockId = this.data.stockId;
    console.log("获取合同："+stockId)
    //todo
    const res = await WXAPI.getContract(
      {
        "stockId":stockId,
      }
    );
    console.log("合同获取结果："+JSON.stringify(res.data))
    this.setData({
      jiafang : res.data[0],
      yifang: res.data[1],
      leixing: res.data[2],
      shuliang: res.data[3],
      riqi: res.data[4],
      guanlifei: "管理费：0.1元每千克/天"
    })
    // if(res.code == 20000){
    //   wx.showModal({
    //     title: '水果入库合同',
    //     content: res.data,
    //     success: async function (res) {
          
    //       if (res.confirm) {
    //         
    //       } else if (res.cancel) {
            
    //         
    //       }
    //       this.getStockList();
    //     }
    //   })
      
    //}
  },

  async accept(){
    const confirmRes = await WXAPI.confirmContract({
                "stockId":this.data.stockId,
              })
              if(confirmRes.code == 20000){
                wx.showModal({
                  title: '成功',
                  content: "签订合同成功",
                  showCancel: false,
                  success(res){
                    wx.redirectTo({
                      url: "/pages/seller/stock-info/index"
                      })
                  }
                })
              }
           
  },

  async refuse(){
    const cancelRes = await WXAPI.cancelContract({
                "stockId":this.data.stockId,
              })
              console.log('用户取消签订合同'+JSON.stringify(cancelRes))
              if(cancelRes.code == 20000){
                wx.showModal({
                  title: '成功',
                  content: "取消签订合同",
                  showCancel: false,
                  success(res){
                    wx.redirectTo({
                     
                      url: "/pages/seller/stock-info/index"
                      })
                  }
                })
                
              }
              
  }
})